package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    /**
     * 分页查询护理项目列表
     *
     * @param name     护理项目名称
     * @param status   状态：0-禁用，1-启用
     * @param pageNum  当前页码
     * @param pageSize 每页显示数量
     * @return 护理项目列表
     */

    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        //1.设置分页查询页码
        PageHelper.startPage(pageNum, pageSize);
        //2.Mapper层完成分页查询
        Page<NursingProjectVo> page =  nursingProjectMapper.selectPage(name,status);
        //pageResponse的封装 以前的写法是 return PageResult(page.getTotal,page.getResult)
        return PageResponse.of(page,NursingProjectVo.class);
    }
    /**
     * 新增护理项目
     * @param nursingProjectDto
     */
    @Override
    public void add(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDto, nursingProject);

        nursingProject.setCreateTime(new Date());
        nursingProject.setUpdateTime(new Date());
        nursingProjectMapper.insert(nursingProject);
    }

    /**
     * 根据Id查询护理项目
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo getById(Long id) {
        return nursingProjectMapper.selectById(id);
    }

    /**
     * 根据Id修改数据
     * @param nursingProjectDto
     */
    @Override
    public void update(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.update(nursingProject);
    }

    /**
     * 启用或者停用护理项目
     * @param status
     * @param id
     */
    @Override
    public void startOrstop(Integer status, Long id) {
        //用来构造新的对象 让局部数据封装完成 其他未封装的数据默认为空Null
        NursingProject nursingProject = NursingProject.builder()
                .id(id)
                .status(status)
                .build();
        nursingProjectMapper.update(nursingProject);
    }

    /**
     * 根据Id删除护理项目
     * @param id
     */
    @Override
    public void delete(Long id) {
        nursingProjectMapper.deleteById(id);
    }

    /**
     * 查询所有的护理项目
     * @return
     */
    @Override
    public List<NursingProjectVo> getAll() {
        return nursingProjectMapper.getAll();
    }


}
